package com.example.administrator.hqq.fragment.information;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.administrator.hqq.R;
import com.example.administrator.hqq.View.layout.EmptyLayout;
import com.example.administrator.hqq.activity.post.CommentDetailsActivity;
import com.example.administrator.hqq.activity.post.PostDetailsActivity;
import com.example.administrator.hqq.adapter.information.MessageListAdapter;
import com.example.administrator.hqq.base.MyFragemnt;
import com.example.administrator.hqq.bean.event.InformationEvent;
import com.example.administrator.hqq.bean.information.ForumNews;
import com.example.administrator.hqq.config.LoongsConfig;
import com.example.administrator.hqq.config.UrlFactory;
import com.google.gson.Gson;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.Call;

/**
 * A simple {@link Fragment} subclass.
 */
public class MessageFragment extends MyFragemnt {


    @BindView(R.id.message_list)
    ListView messageList;
    Unbinder unbinder;
    @BindView(R.id.empty)
    EmptyLayout empty;
    @BindView(R.id.SmartRefreshLayout)
    com.scwang.smartrefresh.layout.SmartRefreshLayout SmartRefreshLayout;
    private View inflate;
    private List<ForumNews.DataBean> data;
    private MessageListAdapter messageListAdapter;

    public MessageFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        inflate = inflater.inflate(R.layout.fragment_message, container, false);
        unbinder = ButterKnife.bind(this, inflate);
        EventBus.getDefault().register(this);
        intiview();
        initdata();
        return inflate;
    }

    private void initdata() {
        OkHttpUtils.get().url(UrlFactory.newsGetForumNews()).tag(this)
                .addParams(LoongsConfig.user_id, LoongsConfig.getUser_id())
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (SmartRefreshLayout != null) {
                    SmartRefreshLayout.finishLoadMore();
                    SmartRefreshLayout.finishRefresh();
                }
                if (empty!=null)
                empty.showEmpty(R.drawable.empty_image,"暂无消息内容");
            }
            @Override
            public void onResponse(String response, int id) {
                if (SmartRefreshLayout != null) {
                    SmartRefreshLayout.finishLoadMore();
                    SmartRefreshLayout.finishRefresh();
                }
                Gson gson = new Gson();
                ForumNews forumNews = gson.fromJson(response, ForumNews.class);
                if (forumNews.getCode() == 200 && forumNews.getData().size() >=0) {
                    if (forumNews.getData()==null||forumNews.getData().size()==0){
                        empty.showEmpty(R.drawable.empty_image,"暂无消息内容");
                    }
                    else{
                        empty.hide();
                    }
                    data.clear();
                    data.addAll(forumNews.getData());
                    messageListAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    private void intiview() {
        data = new ArrayList<>();
        messageListAdapter = new MessageListAdapter(getContext(), data);
        messageList.setAdapter(messageListAdapter);
        messageListAdapter.setForumDetails(new MessageListAdapter.ForumDetails() {
            @Override
            public void clickMessage(int position) {
                if ("REPLY".equals(data.get(position).getType()))
                {
                    Intent intent = new Intent(getContext(), PostDetailsActivity.class);
                    intent.putExtra(LoongsConfig.forum_id, data.get(position).getForum_id());
                    intent.putExtra(LoongsConfig.name, data.get(position).getForum_topic_name());
                    intent.putExtra(LoongsConfig.topic, "");
                    intent.putExtra(LoongsConfig.locate_position_id,data.get(position).getReply_id());
                    startActivity(intent);
                }else{
                    Intent intent = new Intent(getContext(), CommentDetailsActivity.class);
                    intent.putExtra(LoongsConfig.name, data.get(position).getForum_topic_name());
                    intent.putExtra(LoongsConfig.reply_id, data.get(position).getReply_id());
                    intent.putExtra(LoongsConfig.forum_id, data.get(position).getForum_id());
                    intent.putExtra(LoongsConfig.locate_position_id,data.get(position).getDiscussion_id());
                    intent.putExtra(LoongsConfig.type,"examine_forum");
                    startActivity(intent);
                }
            }
        });
        //设置刷新动画
        SmartRefreshLayout.setEnableAutoLoadMore(false);
        SmartRefreshLayout.setRefreshHeader(new MaterialHeader(getContext()));
        SmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                //下拉刷新
                initdata();
            }
        });
        SmartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                //上拉加载
                initdata();
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(InformationEvent informationEvent) {
        if ("MESSAGE".equals(informationEvent.getType())) {
            initdata();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }
}
